package com.yvtian.om.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yvtian.om.pojo.OmCustomerEntity;
import com.yvtian.om.service.OmCustomerService;
import com.yvtian.annotation.Log;
import com.yvtian.entity.Result;
import org.springframework.web.bind.annotation.*;
import com.yvtian.entity.StatusCode;
import com.yvtian.enums.OperatorType;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;


/**
 * (om_customer)表控制层
 *
 * @author long
 * @since 2024-05-08 09:38:21
 */
@RestController
@RequestMapping("/api/om/customer")
public class OmCustomerController {
    /**
     * 服务对象
     */
    @Resource
    private OmCustomerService omCustomerService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        return new Result(true,"查询成功", StatusCode.OK,this.omCustomerService.getById(id));
    }

    @GetMapping(value = "/list")
    public Result findByPage(Page<OmCustomerEntity> page, OmCustomerEntity omCustomerEntity) {
        return new Result(true, "查询成功", StatusCode.OK, omCustomerService.findByPage(page,omCustomerEntity));
    }

    /**
     * 新增数据
     *
     * @param omCustomerEntity 实体对象
     * @return 新增结果
     */
    @Log(value = "添加数据",isSave = true,operType = OperatorType.INSERT)
    @PostMapping("insert")
    public Result insert(@RequestBody OmCustomerEntity omCustomerEntity) {
        return new Result(true,"插入成功", StatusCode.OK,this.omCustomerService.save(omCustomerEntity));
    }

    /**
     * 修改数据
     *
     * @param omCustomerEntity 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    public Result update(@RequestBody OmCustomerEntity omCustomerEntity) {
        return new Result(true,"更新成功", StatusCode.OK,this.omCustomerService.updateById(omCustomerEntity));
    }

    /**
     * 删除数据
     *
     * @param idMap 主键结合
     * @return 删除结果
     */
    @PostMapping("/del")
    public Result delete(@RequestBody Map<String, ArrayList<Integer>> idMap) {
        ArrayList<Integer> ids = idMap.get("ids");
        LambdaUpdateWrapper<OmCustomerEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(OmCustomerEntity::getId, ids);
        lambdaUpdateWrapper.set(OmCustomerEntity::getIsDel, 1);
        return new Result(true, "删除成功", StatusCode.OK, this.omCustomerService.update(lambdaUpdateWrapper));
    }
}

